package com.ftdigital.primemovers_operators.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by Curry on 2016-1-11.
 * sharePreference 工具类
 */
public class PreferencesHelper {

    /**
     * 字符串存储
     * @param context
     * @param tabName  存储文件名
     * @param key      键值对-键
     * @param value    键值对-值
     * @return  操作标识
     */
    public static boolean write(Context context, String tabName, String key, String value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(tabName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        return editor.commit();
    }
    /**
     * 整型存储
     * @param context
     * @param tabName  存储文件名
     * @param key      键值对-键
     * @param value    键值对-值
     * @return  操作标识
     */
    public static boolean write(Context context, String tabName, String key, int value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(tabName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    /**
     * 字符串存储
     * @param context
     * @param tabName  存储文件名
     * @param key      键值对-键
     * @param value    键值对-值
     * @return  操作标识
     */
    public static boolean write(Context context, String tabName, String key, boolean value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(tabName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }

    /**
     * 获取字符串数据
     * @param context
     * @param tabName  存储文件名
     * @param key      键值对-键
     * @return  结果字符串
     */
    public static String readStr(Context context, String tabName, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(tabName, Context.MODE_PRIVATE);
        return sharedPreferences.getString(key, "");
    }

    /**
     * 获取字符串数据
     * @param context
     * @param tabName   存储文件名
     * @param key       键值对-键
     * @param defaltValue 如果没有默认返回值
     * @return  结果字符串
     */
    public static String readStrDev(Context context, String tabName, String key, String defaltValue) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(tabName, Context.MODE_PRIVATE);
        return sharedPreferences.getString(key, defaltValue);
    }
    /**
     * 获取字符串数据
     * @param context
     * @param tabName  存储文件名
     * @param key      键值对-键
     * @return  结果字符串
     */
    public static int readInt(Context context, String tabName, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(tabName, Context.MODE_PRIVATE);
        return sharedPreferences.getInt(key, 0);
    }
    /**
     * 获取int数据
     * @param context
     * @param tabName   存储文件名
     * @param key       键值对-键
     * @param defaltValue 如果没有默认返回值
     * @return  结果字符串
     */
    public static int readIntDev(Context context, String tabName, String key, int defaltValue) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(tabName, Context.MODE_PRIVATE);
        return sharedPreferences.getInt(key, defaltValue);
    }
    /**
     * 获取字符串数据
     * @param context
     * @param tabName  存储文件名
     * @param key      键值对-键
     * @return  结果字符串
     */
    public static boolean readBoolean(Context context, String tabName, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(tabName, Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(key, false);
    }
    /**
     * 获取boolean数据
     * @param context
     * @param tabName   存储文件名
     * @param key       键值对-键
     * @param defaltValue 如果没有默认返回值
     * @return  结果字符串
     */
    public static boolean readBoolean(Context context, String tabName, String key, boolean defaltValue) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(tabName, Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(key, defaltValue);
    }

    /**
     *  移除文件中对应健值数据
     * @param context
     * @param tabName
     * @param k         移除健值
     */
    public static void remove(Context context, String tabName, String k) {
        SharedPreferences preference = context.getSharedPreferences(tabName, 0);
        SharedPreferences.Editor editor = preference.edit();
        editor.remove(k);
        editor.commit();
    }

    /**
     *  清理数据存储文件
     * @param cxt
     * @param tabName
     */
    public static void clean(Context cxt, String tabName) {
        SharedPreferences preference = cxt.getSharedPreferences(tabName, 0);
        SharedPreferences.Editor editor = preference.edit();
        editor.clear();
        editor.commit();
    }
}
